package com.company.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @description: some desc
 * @author: ruoan
 * @date: 2020/12/30 19:59
 */
public class TimeServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        //final ByteBuf buffer = ctx.alloc().buffer(4);
        //buffer.writeInt((int)(System.currentTimeMillis() / 1000 + 2208988800L));
        //ChannelFuture f = ctx.writeAndFlush(buffer);
        ChannelFuture f = ctx.writeAndFlush(new UnixTime());
        f.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                assert f == channelFuture;
                ctx.close();
            }
        });



    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
